package org.musicmaster.yulin.business.impl;

import java.util.ArrayList;
import java.util.List;

import org.musicmaster.yulin.business.UserService;
import org.musicmaster.yulin.business.domain.User;
import org.musicmaster.yulin.business.domain.User.Sex;
import org.musicmaster.yulin.ercache.BusinessCacheable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{

	private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Cacheable("userCache")
	@Override
	public User findById(long id) {
		LOG.info("visit business service findById,id:{}",id);
		User user = new User();
		user.setId(id);
		user.setUserName("tony");
		user.setPassWord("******");
		user.setSex(Sex.M);
		user.setAge(32);
		//耗时操作
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return user;
	}

	
	@Override
	public List<User> findByPage(int startIndex, int limit) {
		return null;
	}
	
	@Cacheable("userCache")
	@Override
	public List<User> findBySex(Sex sex) {
		LOG.info("visit business service findBySex,sex:{}",sex);
		List<User> users = new ArrayList<User>();
		for (int i = 0; i < 5; i++) {
			User user = new User();
			user.setId(i);
			user.setUserName("tony"+i);
			user.setPassWord("******");
			user.setSex(sex);
			user.setAge(32+i);
			users.add(user);
		}
		return users;
	}

	@Override
	public List<User> findByAge(int lessAge) {
		// TODO Auto-generated method stub
		return null;
	}
	
	//FIXME 此处将list参数的地址作为key存储，是否有问题？
	@BusinessCacheable 
	@Override
	public List<User> findByUsers(List<User> users) {
		LOG.info("visit business service findByUsers,users:{}",users);
		return users;
	}


	@CacheEvict("userCache")
	@Override
	public boolean update(User user) {
		return true;
	}

	@CacheEvict("userCache")
	@Override
	public boolean deleteById(long id) {
		return false;
	}
	
}
